﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinStudentMS
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string userName = txtUserName.Text.Trim();
            string userPwd = txtUserPwd.Text.Trim();
            if (string.IsNullOrEmpty(userName))
            {
                MessageBox.Show("请输入账号！", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUserName.Focus();
                return;
            }
            if (string.IsNullOrEmpty(userPwd))
            {
                MessageBox.Show("请输入密码！", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUserPwd.Focus();
                return;
            }
            {
                string sql = "select count(1) from UserInfo where UserName=@UserName and UserPwd=@UserPwd";
                SqlParameter[] paras =
                {
                    new SqlParameter("@UserName", userName),
                    new SqlParameter("@UserPwd", userPwd)
                };
                object obj = SqlHelper.ExecuteScalar(sql,paras);
                if (obj == null || obj == DBNull.Value || ((int) obj) == 0)
                {
                    MessageBox.Show("登录账号或密码错误，请检查！", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    int count = (int)obj;
                    MessageBox.Show("登录成功！", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    MainForm mainForm = new MainForm();
                    mainForm.Show();
                    this.Hide();
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            //this.Close(); 主页面情况下使用
            Application.Exit();
        }
    }
}
